/**
 * 
 */
package com.example.helpconnect;

import java.util.List;

import com.example.controller.Service;
import com.example.controller.ServiceStatus;
import com.example.controller.User;
import com.example.model.ServiceCRUD;
import com.example.model.ServiceStatusCRUD;
import com.example.model.UserCRUD;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * @author Joseph
 *
 */
public class RequestedServices extends Activity {

	User user;
	ServiceStatus serv_stat;
	List<ServiceStatus> services;
	ArrayAdapter adapter;
	ListView listView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.requested_services);
	    
	    listView = (ListView) findViewById(R.id.requestedServicesList);
	    
	    Intent intent = getIntent();
	    
	    int userID = intent.getIntExtra("userID", -1);
	    
	    serv_stat = new ServiceStatus(new ServiceStatusCRUD());
	    
	    user = new User(new UserCRUD());		
		user = user.read(userID);
		
		services = serv_stat.readAllStatusesForOtherServices(userID);
		
		adapter = new ArrayAdapter<ServiceStatus>(this, R.layout.simple_list_item, services);
		listView.setEmptyView(findViewById(R.id.noRequestedServicesText));
		listView.setAdapter(adapter);
		
		
		
		// Create a message handling object as an anonymous class.
		OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
		    public void onItemClick(AdapterView parent, android.view.View v, int position, long id) {
		        // Do something in response to the click
		    	
		    	// 1. Instantiate an AlertDialog.Builder with its constructor
				AlertDialog.Builder builder = new AlertDialog.Builder(parent.getContext());

				// 2. Chain together various setter methods to set the dialog characteristics
				builder.setTitle(R.string.requested_service_dialog_title);
				
				Service service = new Service(new ServiceCRUD());
				
				final ServiceStatus requested_service_status = serv_stat.read(services.get(position).getServiceStatusID());
				final int clicked_position = position;
				
				int service_ID = services.get(position).getServiceID();
				
				Service selected_service = service.read(service_ID);
				
				builder.setMessage(selected_service.getDescription());
				
				builder.setPositiveButton(R.string.view, new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			               // User clicked View button
			        	   viewClicked(requested_service_status.getServiceStatusID());
			           }
			       });
				builder.setNegativeButton(R.string.delete, new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			               // User clicked Delete button
			        	   requested_service_status.delete();
			        	   updateList(clicked_position);
			           }
			       });
				
				builder.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			               // User clicked Cancel button
			        	   dialog.dismiss();
			           }
			       });

				// 3. Get the AlertDialog from create()
				AlertDialog dialog = builder.create();
				
				dialog.show();
		    }
		    
		    
		};

		listView.setOnItemClickListener(mMessageClickedHandler);
		
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	public void updateList(int clicked_position)
    {
    	services.remove(clicked_position);
    	adapter.clear();
    	adapter.addAll(services);
    	adapter.notifyDataSetChanged();
    }
	
	public void viewClicked(int ssID){
		Intent intent = new Intent(this, RequestedServiceViewActivity.class);
		intent.putExtra("ssID", ssID);
		startActivity(intent);
	}
	
	@Override
	public void onResume(){
		
		super.onResume();
		
		Intent intent = getIntent();
	    
	    int userID = intent.getIntExtra("userID", -1);
	    
	    services = serv_stat.readAllStatusesForOtherServices(userID);
	    
	    adapter.notifyDataSetChanged();
	}

}
